<template>
  <div class="login" :style="{ 'background-image': `url(${configKeys.backgroundImage.value})` }">
    <div class="login-title">
      <img :src="configKeys.logo.value" alt="" />
      <p>{{ configKeys.title.value }}</p>
      <!-- <div>{{ configKeys.subTitle.value }}</div> -->
    </div>
    <div class="login-main">
      <div class="login-main-item" :class="{ flipped: flipped == 1 }" @mouseover="flippedOver(1)" @mouseout="flippedOut">
        <div class="front">
          <img :src="configKeys.servicePlatformImage.value || '@/assets/images/login_1.png'" alt="" />
          <p>绩效管理平台</p>
        </div>
        <div class="back">
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
            <h2 class="title">绩效管理平台登录</h2>
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号" :maxlength="11">
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                :type="flag ? 'text' : 'password'"
                auto-complete="off"
                placeholder="密码"
                :maxlength="20"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
                <svg-icon
                  slot="suffix"
                  :icon-class="flag ? 'eye-open' : 'eye'"
                  class="el-input__icon input-icon input-passwrod"
                  @click="flag = !flag"
                />
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                style="width: 63%"
                @keyup.enter.native="handleLogin"
                @focus="focus = 1"
                @blur="focus = 0"
              >
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" />
              </div>
            </el-form-item>
            <el-form-item style="width: 100%">
              <el-button :loading="loading" size="medium" type="primary" style="width: 100%" @click.native.prevent="handleLogin">
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div style="float: right" v-if="register">
                <router-link class="link-type" :to="'/register'">立即注册</router-link>
              </div>
            </el-form-item>
            <div class="flex-justify-between" style="margin-bottom: 20px">
              <el-checkbox v-model="loginForm.avoidLoginSign">一周内免登录</el-checkbox>
              <!-- <el-button class="forgot-password" type="text">忘记密码</el-button> -->
            </div>
          </el-form>
        </div>
      </div>
      <!-- <div class="login-main-item" :class="{ flipped: flipped == 2 }" @mouseover="flippedOver(2)" @mouseout="flippedOut">
        <div class="front">
          <img :src="configKeys.statisticsImage.value || '@/assets/images/login_2.png'" alt="" />
          <p>统计分析平台</p>
        </div>
        <div class="back">
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
            <img class="login-logo" src="@/assets/logo/logo.png" alt="" />
            <h2 class="title">统计分析平台登录</h2>
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号" :maxlength="11">
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                :type="flag ? 'text' : 'password'"
                auto-complete="off"
                placeholder="密码"
                :maxlength="20"
                @keyup.enter.native="handleLogin(2)"
              >
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
                <svg-icon
                  slot="suffix"
                  :icon-class="flag ? 'eye-open' : 'eye'"
                  class="el-input__icon input-icon input-passwrod"
                  @click="flag = !flag"
                />
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled">
              <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleLogin(2)">
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" />
              </div>
            </el-form-item>
            <el-form-item style="width: 100%">
              <el-button :loading="loading" size="medium" type="primary" style="width: 100%" @click.native.prevent="handleLogin(2)">
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div style="float: right" v-if="register">
                <router-link class="link-type" :to="'/register'">立即注册</router-link>
              </div>
            </el-form-item>
            <div class="flex-justify-between" style="margin-bottom: 20px">
              <el-checkbox v-model="loginForm.avoidLoginSign">一周内免登录</el-checkbox>
              <el-button class="forgot-password" type="text">忘记密码</el-button>
            </div>
          </el-form>
        </div>
      </div> -->
      <div class="login-main-item" :class="{ flipped: flipped == 3 }" @mouseover="flippedOver(3)" @mouseout="flippedOut">
        <div class="front">
          <img :src="configKeys.miniProgramImage.value || '@/assets/images/login_3.png'" alt="" />
          <p>移动办公云</p>
        </div>
        <div class="back last-back">
          <h2 class="title">移动办公云扫码下载</h2>
          <div class="last-back-main">
            <img :src="configKeys.miniProgramSrc.value" alt="" />
          </div>
        </div>
      </div>
    </div>

    <!--  底部  -->
    <div class="el-login-footer">
      <span style="margin-right: 20px">{{ configKeys.copyright.value }}</span>
      <span style="margin-right: 20px">{{ configKeys.consult.value }}</span>
      <a v-if="configKeys.privacyPolicy.value" :href="configKeys.privacyPolicy.value" target="_blank"><u>隐私政策</u></a>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import { getConfigKey } from '@/api/system/config'

export default {
  name: 'Login',
  data() {
    return {
      focus: 0, // 焦点是否激活
      baseUrl: process.env.VUE_APP_BASE_API,
      flipped: 0,
      flag: false,
      codeUrl: '',
      loginForm: {
        username: '',
        password: '',
        avoidLoginSign: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      // 底部数据
      configKeys: {
        copyright: {
          key: 'sys.copyright',
          value: undefined,
          title: '系统版权所有'
        },
        consult: {
          key: 'sys.consult',
          value: undefined,
          title: '操作系统咨询'
        },
        privacyPolicy: {
          key: 'sys.privacyPolicy',
          value: undefined,
          title: '系统隐私政策'
        },
        title: {
          key: 'sys.website.title',
          value: undefined,
          title: '登录页标题'
        },
        subTitle: {
          key: 'sys.website.subTitle',
          value: undefined,
          title: '登录页副标题'
        },
        miniProgramSrc: {
          key: 'miniProgram.QRCode',
          value: '#',
          title: '湘渔云小程序二维码'
        },
        visualizationUrl: {
          key: 'sys.visualization.url',
          value: undefined,
          title: '智慧大屏地址'
        },
        backgroundImage: {
          key: 'sys.website.backgroundImage',
          value: undefined,
          title: '背景大图'
        },
        servicePlatformImage: {
          key: 'sys.website.servicePlatformImage',
          value: undefined,
          title: '绩效管理平台进入图'
        },
        statisticsImage: {
          key: 'sys.website.statisticsImage',
          value: undefined,
          title: '统计一张图进入图'
        },
        miniProgramImage: {
          key: 'sys.website.miniProgramImage',
          value: undefined,
          title: '小程序进入图'
        },
        logo: {
          key: 'sys.website.logo',
          value: undefined,
          title: '网站logo'
        }
      }
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
    this.getConfigKey()
  },
  methods: {
    flippedOver(index) {
      this.flipped = index
    },
    flippedOut() {
      if (this.focus) return
      this.flipped = 0
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = 'data:image/gif;base64,' + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const avoidLoginSign = Cookies.get('avoidLoginSign')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        avoidLoginSign: avoidLoginSign === undefined ? false : Boolean(avoidLoginSign)
      }
    },
    handleLogin(status) {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.avoidLoginSign) {
            Cookies.set('username', this.loginForm.username, { expires: 7 })
            Cookies.set('password', encrypt(this.loginForm.password), {
              expires: 7
            })
            Cookies.set('avoidLoginSign', this.loginForm.avoidLoginSign, {
              expires: 7
            })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('avoidLoginSign')
          }
          this.$store
            .dispatch('Login', this.loginForm)
            .then(() => {
              if (status == 2) {
                window.location.href = this.configKeys.visualizationUrl.value
                // window.open(this.configKeys.visualizationUrl.value,"_blank");
              } else {
                this.$router.push({ path: this.redirect || '/' }).catch(() => {})
              }
            })
            .catch(() => {
              this.loading = false
              if (this.captchaEnabled) {
                this.getCode()
              }
            })
        }
      })
    },
    async getConfigKey() {
      const promiseArr = []
      for (const key in this.configKeys) {
        promiseArr.push(
          (() => {
            const item = this.configKeys[key]
            return new Promise((resolve, reject) => {
              getConfigKey(item.key).then(res => {
                item.value = res.msg || ''
                resolve(item)
              })
            })
          })()
        )
      }
      return await Promise.all(promiseArr)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  // position: relative;
  width: 100%;
  height: 100%;
  background-image: url('../assets/images/login-background.jpg');
  background-size: cover;
  display: flex;
  flex-direction: column;
}

.login-title {
  color: #333;
  text-align: center;
  font-size: 58px;
  font-weight: 700;
  // text-decoration: underline;
  color: #f5f5f5;
  p {
    margin: 0;
    margin-top: 20px;
  }
  img {
    margin-top: 20px;
    width: 130px;
    height: 130px;
  }
}

.login-main {
  width: 100%;
  // height: 600px;
  // min-height: 700px;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .login-main-item {
    width: 400px;
    height: 400px;
    position: relative;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;

    .front {
      width: 300px;
      height: 300px;
      // border-radius: 50%;
      font-size: 24px;
      font-weight: bold;
      color: #333;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      transition: all 1s ease-out;

      & > img {
        width: 150px;
        height: 150px;
      }
    }

    .back {
      display: flex;
      align-items: center;
      justify-content: center;
      transition: all 1s ease-out;

      .login-form {
        border-top: 10px solid #136fcc;
        width: 400px;
        height: 100%;
        box-sizing: border-box;
        padding: 25px 25px 5px 25px;
        .login-logo {
          display: block;
          width: 80px;
          height: 80px;
          margin: 0 auto 20px;
        }
        .el-input {
          height: 38px;
          input {
            height: 38px;
          }
        }
        .input-icon {
          height: 39px;
          width: 14px;
          margin-left: 2px;
        }
        .forgot-password {
          padding: 0;
          height: 100%;
          color: #606266;
        }
        .input-passwrod {
          cursor: pointer;
        }
      }
    }
    .last-back {
      width: 100%;
      height: 100%;
      border-radius: 6px;
      background-color: #fff;
      border-top: 10px solid #136fcc;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      box-sizing: border-box;
      padding: 25px 25px 5px 25px;
      .title {
        flex-shrink: 0;
        margin-bottom: 0 !important;
      }
      .last-back-main {
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        & > img {
          width: 220px;
          height: 220px;
        }
      }
    }
  }
}
.front,
.back {
  width: 100%;
  height: 100%;
  border-radius: 6px;
  position: absolute;
  backface-visibility: hidden;
  background: rgba($color: #fff, $alpha: 0.9);
  overflow: hidden;
}

.front {
  transform: rotateY(0deg);
  transition: transform 0.8s;
}

.back {
  transform: rotateY(-180deg);
  transition: transform 0.8s;
}

.flipped .front {
  transform: rotateY(180deg);
}

.flipped .back {
  transform: rotateY(0deg);
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  text-align: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  // height: 30px;
  line-height: 30px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
  font-weight: 700;
}
</style>
